clear all
set more off

use  "../data/decile_year_month_data",replace

gen top = decile_chi_pop >= 9

collapse (sum) ct sr_val_tran chinese100* hispanic100* indian100* russian100* korean100* japanese100* american100* european100* , by(monthdate top)

tsset top month
sort month top

*calculate the difference in adjusted FHTV/FHTC percents between zip codes in the top 2 deciles of historical Chinese population share and those in the bottom 8 deciles
foreach r in hispanic indian russian korean japanese european chinese{

gen `r'_cashvalall_percent = max((`r'100_cashval_tran - american100_cashval/american100_val*`r'100_val)*(american100_val/(american100_val - american100_cashval))/sr_*100,0.01)	
gen `r'_cashvalall_percent_iq = `r'_cashvalall_percent - `r'_cashvalall_percent[_n-1] if top == 1

gen `r'_cashctall_percent = max((`r'100cash - american100cash/american100*`r'100)*(american100/(american100 - american100cash))/ct*100,0.01)
gen `r'_cashctall_percent_iq = `r'_cashctall_percent - `r'_cashctall_percent[_n-1] if top == 1
}


twoway (tsline chinese_cashvalall_percent_iq if top == 1, lwidth(thick)) (tsline hispanic_cashvalall_percent_iq if top == 1) (tsline indian_cashvalall_percent_iq if top == 1) ///
(tsline russian_cashvalall_percent_iq if top == 1) (tsline korean_cashvalall_percent_iq if top == 1) (tsline japanese_cashvalall_percent_iq if top == 1)  ///
(tsline european_cashvalall_percent_iq if top == 1) ///
, legend(order(1 "Chinese" 2 "Hispanic" 3 "Indian" 4 "Russian" 5 "Korean" 6 "Japanese" 7 "European") rows(3)) ///
graphregion(color(white)) bgcolor(white) xlabel(492(24)647) ylabel(-1(1)6) ttitle("")
graph export "../results/figure3a.png", replace

twoway (tsline chinese_cashctall_percent_iq if top == 1, lwidth(thick)) (tsline hispanic_cashctall_percent_iq if top == 1) (tsline indian_cashctall_percent_iq if top == 1) ///
 (tsline russian_cashctall_percent_iq if top == 1) (tsline korean_cashctall_percent_iq if top == 1) (tsline japanese_cashctall_percent_iq if top == 1) ///
 (tsline european_cashctall_percent_iq if top == 1) ///
 , legend(order(1 "Chinese" 2 "Hispanic" 3 "Indian" 4 "Russian" 5 "Korean" 6 "Japanese" 7 "European") rows(3)) ///
graphregion(color(white)) bgcolor(white) xlabel(492(24)647) ylabel(-1(1)6) ttitle("")
graph export "../results/figure3b.png", replace
